### REPLICATION FILE -- STUDY 2, PERCEPTIONS -- FIGURE 3
### Homola, Rogowski, Sinclair, Torres, Tucker, and Webster
### "Through the Ideology of the Beholder: How Ideology Shapes Perceptions of Partisan Groups"
### Political Science Research and Methods

rm(list=ls())

data <- read.csv("HRSTTW_Stereotypes_Study2_Appendix.csv")

mygray <- rgb(col2rgb("gray80")[1], col2rgb("gray80")[2], col2rgb("gray80")[3], alpha = 100, maxColorValue = 255) 

dem_data<-subset(data, PID3_MAXN=="Democrat")
rep_data<-subset(data, PID3_MAXN=="Republican")
ind_data<-subset(data, PID3_MAXN=="Independent/Other")

# the following estimates are produced at the beginning of HRSTTW_Stereotypes_Study2_MainAnalysis.do

# Republican Beliefs
rep_belief_estimates<-c( 2.612888, 2.697005, 2.781121, 2.865238, 2.949355, 3.033471, 3.117588, 3.201705 ,3.285822, 3.369938, 3.454055, 3.538172)
rep_belief_low_ci<-c(2.289358, 2.39239, 2.492152, 2.588092, 2.679705, 2.766627, 2.84871 , 2.926063, 2.999019, 3.068066, 3.133755,3.196629)
rep_belief_high_ci<-c(2.936418, 3.00162, 3.070091, 3.142385, 3.219005, 3.300316, 3.386466,3.477347,3.572624, 3.67181,  3.774355,  3.879714)
rep_belief_ideo_values<-c(seq(-.5, 2.25, by=.25))

# Democratic Beliefs
dem_belief_estimates<-c(3.73625, 3.643862, 3.551474, 3.459085, 3.366697, 3.274309, 3.18192, 3.089532,  2.997144, 2.904755 , 2.812367 , 2.719979)
dem_belief_low_ci<-c( 3.468857, 3.398715,  3.3256, 3.248696,3.167117 , 3.080082, 2.98714, 2.888341,  2.784211, 2.67557, 2.563299, 2.448193)
dem_belief_high_ci<-c(4.003643, 3.889009,  3.777347, 3.669475, 3.566277, 3.468535, 3.3767, 3.290723, 3.210076,  3.13394, 3.061435, 2.991764)
dem_belief_ideo_values<-c(seq(-2.25, .5, by=.25))

pdf("Figure3_BeliefsIdeol_mode.pdf")
layout(matrix(c(1,2), ncol=1, byrow = TRUE), heights = c(0.8,0.2))
par(mar=c(2.5, 3, 1, 1), oma=c(0,2,0,1))
plot(rep_belief_ideo_values, rep_belief_estimates, type="n",
     xlim=c(-2,2), ylim=c(2,4),
     xaxt="n", yaxt="n", xlab="", ylab="",
     main="",
     cex=0.9)
polygon(c(rep_belief_ideo_values,rev(rep_belief_ideo_values)),
        c(rep_belief_low_ci,rev(rep_belief_high_ci)), col=mygray, border=NA)
lines(rep_belief_ideo_values, rep_belief_estimates)
polygon(c(dem_belief_ideo_values,rev(dem_belief_ideo_values)),
        c(dem_belief_low_ci,rev(dem_belief_high_ci)), col=mygray, border=NA)
lines(dem_belief_ideo_values, dem_belief_estimates, lty=2)
rug(dem_data$ideoS27_std, ticksize = 0.03, side = 1, lwd = 0.5, col = par("fg"),
    quiet = getOption("warn") < 0)
rug(rep_data$ideoS27_std, ticksize = 0.03, side = 3, lwd = 0.5, col = par("fg"),
    quiet = getOption("warn") < 0)
axis(1, tck=0, cex.axis=0.7, cex.lab=0.7, mgp=c(0.5, 0.3, 0), lty=0, labels=TRUE)
axis(2, tck = -0.3, lwd = 0, cex.axis = 0.7, cex.lab = 0.9, mgp=c(0.5, 0.3, 0), 
     lty=0)
title(xlab = "Ideology", line = 1.5, cex.lab = 0.8, ylab="Perceived Agreement")
text(1.8, 3.95, "Republicans")
text(-1.8, 2.05, "Democrats")
##### LEGEND
par(mar=c(0, 0, 0, 0))
plot(0,0, type="n", axes = FALSE, xlab = "", ylab = "")
legend("center", ncol = 2, 
       legend = c("Dems. beliefs about Reps.","Reps. beliefs about Dems."),
       cex=0.9, lty=c(2,1)) 
dev.off()